/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package iteracon.tuple;

import iteracon.tuple.iterator.TupleIterator;
import java.util.Iterator;
import java.util.List;
import iteracon.IteraconBase;
import java.util.ListIterator;
import iteracon.tuple.iterator.TupleListIterator;

/**
 * tuple , simple immutable array
 * @author martin
 */
public final class Tuple<T> extends IteraconBase<T> implements Tpl<T>{
  private final T [] _arr;
  /**
   * constructor
   * @param objs any array of collection 
   */
  private Tuple(T ... objs){
    _arr = objs;
  }
  /**
   * static factory
   * @param <T> as type
   * @param objs as sequence
   * @return new Tuple as List
   */
  public static<T> List<T> make(Object ... objs){
    return new Tuple(objs);
  }

  //tpl
  @Override
  public T get(int i) {
    return _arr[i];
  }

  @Override
  public int size() {
    return _arr.length;
  }
  
  @Override
  public Iterator<T> iterator() {
    return new TupleIterator(this);
  }

  @Override
  public ListIterator<T> listIterator() {
    return new TupleListIterator(this,0);
  }

  @Override
  public ListIterator<T> listIterator(int index) {
    return new TupleListIterator(this,index);
  }

  @Override
  public List<T> subList(int fromIndex, int toIndex) {
    final Object [] tmp = new Object[toIndex-fromIndex]; 
    System.arraycopy(_arr, fromIndex, tmp, 0, toIndex);
    return Tuple.make(tmp);
  }
  
}
